﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using FlyComet.Silverlight;

namespace SilverlightSample
{
    public partial class MainPage : UserControl
    {
        FlyCometSilverlight fs = null;
        public MainPage()
        {
            InitializeComponent();
            fs = new FlyCometSilverlight(this);
            fs.Connected += new FlyCometSilverlight.ConnectionDelegate(fs_Connected);
            textBox1.Text = "1111";
        }

        void fs_Connected(bool connected)
        {
            if (connected)
                AppendTextToTextBlock("Connected Successfully.");
            else
                AppendTextToTextBlock("Connection Error.");
                
        }

        void AppendTextToTextBlock(String msg)
        {
            this.Dispatcher.BeginInvoke(delegate()
            {
                textBlock2.Text += textBlock2.Text + "\r" + msg;
            });
        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            fs.LiveUpdateReceived += new FlyCometSilverlight.LiveUpdateDelegate(fs_LiveDataReceived);
            bool bret = fs.Connect();
        }

        void fs_LiveDataReceived(FlyComet.LiveMessage msg)
        {
            this.Dispatcher.BeginInvoke(delegate()
            {
//                MessageBox.Show(msg.Message);
                AppendTextToTextBlock(msg.Message);
            });
        }

        private void button2_Click(object sender, RoutedEventArgs e)
        {
            fs.Subscribe(textBox1.Text);
        }
    }
}
